package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.item.dao.model.Book;
import com.item.dao.model.Cart;
import com.item.dao.model.CartExample;
import com.item.daoEx.Ret;
import com.item.exception.ActionException;
import com.item.exception.JSONRepUtils;
import com.item.service.BookService;
import com.item.service.CartService;

@Controller
public class CartController {
	@Autowired
	private CartService cartService;
	@Autowired
	private BookService bookService;
	
	@RequestMapping("/listCart")
	@ResponseBody
	public String list(Page param, Cart cart, Integer isNotPage, Integer isM) throws Exception{
		Object objForSer = null;
		CartExample example = new CartExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);
		CartExample.Criteria criteria = example.createCriteria();
		
		if(StringUtils.isNotBlank(cart.getUserid())) {
			criteria.andUseridEqualTo(cart.getUserid());
		}
		example.setOrderByClause("create_time DESC");
		List<Cart> list = cartService.selectByExample(example);
		
		if(isNotPage != null && isNotPage == 1) {
			objForSer = list;
		} else {
			objForSer = new Page(PaginationSupport.getContext().getTotalCount(), list);
		}
		
		return JSONRepUtils.serialize(objForSer, isM);
	}
	
	@RequestMapping("/showlistCart")
	public String list() {
		return "cart/cartList";
	}
	
	@RequestMapping("/toEditCart")
	public String toEdit()throws Exception{
		return "cart/cartEdit";
	}
	
	@RequestMapping(value="/saveCart")
	@ResponseBody
	public String save(Cart cart, Integer isM, HttpSession session) throws Exception{		
		if(cart.getCount() == null) {
			new ActionException("count");
		}
		Ret ret = new Ret();
		Book book = bookService.selectByPrimaryKey(cart.getBookid());
		if(book != null) {
			cart.setBookName(book.getName());
			cart.setBookImg(book.getImg());
			cart.setPrice(book.getPriceSelling());
			cart.setPriceTotal(book.getPriceSelling() * cart.getCount());
			cart.setModifyTime(new Date());
			int rows = cartService.updateByPrimaryKeySelective(cart);
			if(rows == 0) {
				cart.setCreateTime(new Date());
				rows = cartService.insertSelective(cart);
			}
			if(rows > 0)
				ret.setSuccess(true);
		}
		return JSONRepUtils.serialize(ret, isM);
	}
	
//	@RequestMapping(value="/findByIdCart")
//	@ResponseBody
//	public String findById(String id,HttpSession session)throws Exception{		
//		Cart n = cartService.selectByPrimaryKey(id);
//
//		return JSONUtils.serialize(n);
//	}
	
	@RequestMapping("/delCart")
	@ResponseBody
	public String del(Cart cart, Integer isM, HttpSession session)throws Exception{
		Ret ret = new Ret();
		
		int rows = cartService.deleteByPrimaryKey(cart);
		if(rows > 0) {
			ret.setSuccess(true);
		}
//		
		return JSONRepUtils.serialize(ret, isM);
	}
	
	
}

